package com.yunpeng.aidevice.common.controllers.configs;

import com.alibaba.fastjson.JSONObject;
import com.yunpeng.aidevice.biz.service.IOrganizationBizService;
import com.yunpeng.aidevice.common.constant.ConfigConstantValues;
import com.yunpeng.aidevice.common.dto.cache.LoginUserCacheDTO;
import com.yunpeng.aidevice.common.dto.save.OrgSaveDTO;
import com.yunpeng.aidevice.common.vo.SimpleEntityVO;
import com.yunpeng.aidevice.common.vo.org.OrganizationNodeVO;
import com.yunpeng.aidevice.common.vo.org.OrganizationRootVO;
import com.yunpeng.aidevice.common.vo.org.OrganizationVO;
import com.yunpeng.common.dto.Result;
import com.yunpeng.common.util.ParametersCheckUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * OrganizationController
 *
 * @author : Moore(hemw)
 * @date : 2020-04-09
 */
@RestController
@RequestMapping("/api/organization")
public class OrganizationController {

    @Resource
    private IOrganizationBizService organizationBizService;

    /**
     * 获取当前登录用户的顶级组织节点信息
     *
     * @return 顶级节点信息
     */
    @ResponseBody
    @RequestMapping(value = "getRootInfo", method = RequestMethod.POST)
    public Result<OrganizationRootVO> getRootInfo() {
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        return organizationBizService.getRoot(loginUserCacheDTO.getCode(), loginUserCacheDTO.getMerchantCode(), loginUserCacheDTO.getOrganizationPath());
    }

    /**
     * 新增组织机构
     *
     * @param orgSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "addNew", method = RequestMethod.POST)
    public Result<String> addNew(@RequestBody OrgSaveDTO orgSaveDTO) {
        ParametersCheckUtil.isNotNull(orgSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotBlank(orgSaveDTO.getName(), "组织名称为空");
        ParametersCheckUtil.isNotBlank(orgSaveDTO.getNumber(), "组织编号为空");
        ParametersCheckUtil.isNotBlank(orgSaveDTO.getPath(), "组织路径为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        orgSaveDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        orgSaveDTO.setSimpleName(orgSaveDTO.getName());
        return organizationBizService.addNew(orgSaveDTO, loginUserCacheDTO.getCode());
    }

    /**
     * 组织机构树
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "treeList", method = RequestMethod.POST)
    public Result<OrganizationNodeVO> treeList(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String nodeCode = requestParam.getString("nodeCode");
        ParametersCheckUtil.isNotBlank(nodeCode, "节点代码为空");
        return organizationBizService.getOrganizationTree(nodeCode, ConfigConstantValues.getLoginUserCacheDTO().getMerchantCode());
    }

    /**
     * 删除组织
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "removeOne", method = RequestMethod.POST)
    public Result<Void> removeOne(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organization = requestParam.getString("organization");
        ParametersCheckUtil.isNotBlank(organization, "节点代码为空");
        return organizationBizService.removeOrganization(organization, ConfigConstantValues.getLoginUserCacheDTO().getMerchantCode());
    }

    /**
     * 启用组织
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public Result<Void> enable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organization = requestParam.getString("organization");
        ParametersCheckUtil.isNotBlank(organization, "节点代码为空");
        return organizationBizService.enableOrganization(organization, ConfigConstantValues.getLoginUserCacheDTO().getMerchantCode(), ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 禁用组织
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public Result<Void> disable(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organization = requestParam.getString("organization");
        ParametersCheckUtil.isNotBlank(organization, "节点代码为空");
        return organizationBizService.disableOrganization(organization, ConfigConstantValues.getLoginUserCacheDTO().getMerchantCode(), ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }


    /**
     * 禁用组织
     *
     * @param orgSaveDTO
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public Result<Void> modify(@RequestBody OrgSaveDTO orgSaveDTO) {
        ParametersCheckUtil.isNotNull(orgSaveDTO, "没有传递任何参数");
        ParametersCheckUtil.isNotBlank(orgSaveDTO.getName(), "组织名称为空");
        ParametersCheckUtil.isNotBlank(orgSaveDTO.getNumber(), "组织编号为空");
        ParametersCheckUtil.isNotBlank(orgSaveDTO.getPath(), "组织路径为空");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        orgSaveDTO.setMerchantCode(loginUserCacheDTO.getMerchantCode());
        orgSaveDTO.setSimpleName(orgSaveDTO.getName());
        return organizationBizService.updateOrganization(orgSaveDTO, ConfigConstantValues.getLoginUserCacheDTO().getCode());
    }

    /**
     * 组织详情
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "detail", method = RequestMethod.POST)
    public Result<OrganizationVO> detail(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String organization = requestParam.getString("organization");
        ParametersCheckUtil.isNotBlank(organization, "节点代码为空");
        return organizationBizService.getOrganizationDetail(organization, ConfigConstantValues.getLoginUserCacheDTO().getMerchantCode());
    }


    /**
     * 组织下拉数据
     *
     * @param requestParam
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "dropDownList", method = RequestMethod.POST)
    public Result<List<SimpleEntityVO>> dropDownList(@RequestBody JSONObject requestParam) {
        ParametersCheckUtil.mapIsNotNullOrderEmpty(requestParam, "没有传递任何参数");
        String merchantCode = requestParam.getString("merchantCode");
        LoginUserCacheDTO loginUserCacheDTO = ConfigConstantValues.getLoginUserCacheDTO();
        if (StringUtils.isBlank(merchantCode)) {
            merchantCode = loginUserCacheDTO.getMerchantCode();
        }
        return organizationBizService.dropDownList(merchantCode, loginUserCacheDTO.getTenantCode());
    }

}
